<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty 协议编解码深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 2px;
        }
        
        .flow-card {
            background: white;
            border-radius: 16px;
            padding: 24px;
            position: relative;
            overflow: hidden;
        }
        
        .flow-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 20px 0;
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .step-number {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            flex-shrink: 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl font-bold mb-6">Netty 协议编解码深度解析</h1>
            <p class="text-xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索 Netty 高效网络通信架构的核心机制，深入理解编解码器的设计与实现
            </p>
            <div class="mt-10 flex justify-center gap-8">
                <div class="text-center">
                    <i class="fas fa-code text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">自定义编解码</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-exchange-alt text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">双向转换</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-rocket text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">高性能传输</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <p class="text-gray-700 leading-relaxed text-lg indent-8">
                Netty 的协议编解码是其高效网络通信架构的重要组成部分。通过自定义编解码器（Codec），Netty 可以处理各种网络协议的传输。Netty 主要使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ChannelHandler</code> 来实现编解码功能，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ChannelHandler</code> 是 Netty 的核心概念之一，它用于处理 I/O 操作、消息编解码、异常处理等。
            </p>
        </div>

        <!-- Section 1: 基本概念 -->
        <div class="mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-lightbulb text-purple-600 mr-3"></i>1. Netty 编解码的基本概念
            </h2>
            
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-6">
                <p class="text-gray-700 leading-relaxed mb-6 indent-8">
                    Netty 的协议编解码主要分为两部分：编码（Encoder）和解码（Decoder）。编码是将 Java 对象转换为字节流（即网络传输的二进制数据），解码则是将字节流转换回 Java 对象。
                </p>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="flow-card card-hover">
                        <h3 class="text-xl font-bold text-gray-800 mb-3">
                            <i class="fas fa-upload text-blue-500 mr-2"></i>编码器（Encoder）
                        </h3>
                        <p class="text-gray-600">
                            负责将应用层的数据（通常是 Java 对象）转换为字节数组（<code class="bg-blue-100 text-blue-800 px-2 py-1 rounded text-sm">ByteBuf</code>）以便通过网络发送。
                        </p>
                    </div>
                    
                    <div class="flow-card card-hover">
                        <h3 class="text-xl font-bold text-gray-800 mb-3">
                            <i class="fas fa-download text-green-500 mr-2"></i>解码器（Decoder）
                        </h3>
                        <p class="text-gray-600">
                            负责将接收到的字节数组（<code class="bg-green-100 text-green-800 px-2 py-1 rounded text-sm">ByteBuf</code>）转换回应用层的 Java 对象。
                        </p>
                    </div>
                </div>
            </div>

            <!-- 接口定义代码 -->
            <div class="code-block mb-8">
                <div class="code-header">
                    <div class="code-dots">
                        <span class="dot dot-red"></span>
                        <span class="dot dot-yellow"></span>
                        <span class="dot dot-green"></span>
                    </div>
                    <span>Java</span>
                </div>
                <pre><code>public interface MessageToByteEncoder&lt;I&gt; {
    void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception;
}

public interface ByteToMessageDecoder {
    void decode(ChannelHandlerContext ctx, ByteBuf in, List&lt;Object&gt; out) throws Exception;
}</code></pre>
            </div>
        </div>

        <!-- Section 2: 编码器实现 -->
        <div class="mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-code text-purple-600 mr-3"></i>2. 编码器（Encoder）实现
            </h2>
            
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-6">
                <p class="text-gray-700 leading-relaxed mb-6 indent-8">
                    编码器的核心任务是将 Java 对象转换为字节流。Netty 提供了 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">MessageToByteEncoder</code> 作为编码器的基础类。通过实现 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">encode()</code> 方法，可以将应用层的对象转换为字节流，并写入 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ByteBuf</code>。
                </p>
            </div>

            <!-- 编码器示例代码 -->
            <div class="code-block mb-8">
                <div class="code-header">
                    <div class="code-dots">
                        <span class="dot dot-red"></span>
                        <span class="dot dot-yellow"></span>
                        <span class="dot dot-green"></span>
                    </div>
                    <span>MyMessageEncoder.java</span>
                </div>
                <pre><code>public class MyMessageEncoder extends MessageToByteEncoder&lt;MyMessage&gt; {
    @Override
    protected void encode(ChannelHandlerContext ctx, MyMessage msg, ByteBuf out) throws Exception {
        // 将MyMessage对象转换为字节流
        out.writeInt(msg.getId());
        out.writeCharSequence(msg.getName(), CharsetUtil.UTF_8);
    }
}</code></pre>
            </div>

            <!-- 执行流程 -->
            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8">
                <h3 class="text-2xl font-bold text-gray-800 mb-6">
                    <i class="fas fa-stream text-purple-600 mr-2"></i>执行流程
                </h3>
                <div class="space-y-4">
                    <div class="flex items-start gap-4">
                        <div class="step-number">1</div>
                        <div>
                            <h4 class="font-bold text-gray-800 mb-2">encode() 方法</h4>
                            <p class="text-gray-600">在 encode() 方法中，开发者根据自定义协议，将应用层的 Java 对象转换为字节流，并通过 out 写入到 ByteBuf。</p>
                        </div>
                    </div>
                    <div class="flex items-start gap-4">
                        <div class="step-number">2</div>
                        <div>
                            <h4 class="font-bold text-gray-800 mb-2">数据写入</h4>
                            <p class="text-gray-600">ByteBuf 是 Netty 的数据容器，通过调用 writeInt() 和 writeCharSequence() 等方法，将数据写入到 ByteBuf 中。</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Section 3: 解码器实现 -->
        <div class="mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-puzzle-piece text-purple-600 mr-3"></i>3. 解码器（Decoder）实现
            </h2>
            
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-6">
                <p class="text-gray-700 leading-relaxed mb-6 indent-8">
                    解码器的核心任务是将字节流转换为 Java 对象。Netty 提供了 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ByteToMessageDecoder</code> 类作为解码器的基础类。通过实现 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">decode()</code> 方法，可以从接收到的字节流中读取数据并转换为应用层的对象。
                </p>
            </div>

            <!-- 解码器示例代码 -->
            <div class="code-block mb-8">
                <div class="code-header">
                    <div class="code-dots">
                        <span class="dot dot-red"></span>
                        <span class="dot dot-yellow"></span>
                        <span class="dot dot-green"></span>
                    </div>
                    <span>MyMessageDecoder.java</span>